<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img src="../../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
      <el-form
        class="login-form"
        :rules="rules"
        :model="form"
        label-width="0px"
        ref="loginFormRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
          <!-- 密码 -->
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetFileds">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 这是登录表单的数据绑定对象
      form: {
        username: "",
        password: "",
      },
      //   这是表单的验证规则对象
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
            trigger: "blur",
          },
          {
            min: 5,
            max: 10,
            message: "用户名长度在5~10为之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
          {
            min: 6,
            max: 12,
            message: "密码长度在6~12为之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //   重置事件
    resetFileds() {
      this.$refs.loginFormRef.resetFields();
    },
    //   点击登录按钮
    login() {
      // 先对文本框进行预校验,如果不行就不能登陆
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        }
        let { data: res } = await this.$axios.post("login", this.form);
        if (res.meta.status != 200) {
           this.$message.error('登录失败');
          return;
        }
        this.$message({
          message: "登录成功",
          type: "success",
        });
        // 登录成功之后将后台返回的token保存在sessionStorage中
        window.sessionStorage.setItem('token',res.data.token)
        this.$router.push('/home')
      });
    },
  },
};
</script>
<style lang="less" scoped>
.login-container {
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login-form {
  position: absolute;
  bottom: 0px;
  left: 0;
  box-sizing: border-box;
  padding: 20px;
  width: 100%;
  .btn {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
}
</style>